<?php

namespace app\modules\Admin\models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;
use memberModel;

class memberwalletapplyModel extends Model
{
    protected $table = 'member_wallet_apply';
    protected $primaryKey = 'id';
    protected $appends = ['status_name', 'apply_type_text', 'platform_text', 'apply_info_text'];

    protected $hidden = ['platform', 'apply_type', 'apply_info'];
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    // 分销佣金状态  table: commission_reward, field: status
    const STATUS_WAITING = 0;  // 待审核
    const STATUS_PROCESSING = 1;  // 处理中
    const STATUS_SOLVED = 2;  // 已打款
    const STATUS_REFUSE = -1;  // 已拒绝

    public function getStatusNameAttribute()
    {
        $status_name = '';

        switch ($this->attributes['status']) {
            case self::STATUS_WAITING:
                $status_name = '待审核';
                break;
            case self::STATUS_PROCESSING:
                $status_name = '处理中';
                break;
            case self::STATUS_SOLVED:
                $status_name = '已打款';
                break;
            case self::STATUS_REFUSE:
                $status_name = '已拒绝';
                break;
        }

        return $status_name;
    }

    public function getApplyTypeTextAttribute()
    {
        $apply_type_text = '';
        switch ($this->attributes['apply_type']) {
            case 'bank':
                $apply_type_text = '银行卡';
                break;
            case 'wechat':
                $apply_type_text = '微信零钱';
                break;
            case 'alipay':
                $apply_type_text = '支付宝';
                break;
        }

        return $apply_type_text;
    }

    public function getPlatformTextAttribute()
    {
        $platform_text = '';
        switch ($this->attributes['platform']) {
            case 'H5':
                $platform_text = 'H5';
                break;
            case 'wxOfficialAccount':
                $platform_text = '微信公众号';
                break;
            case 'wxMiniProgram':
                $platform_text = '微信小程序';
                break;
            case 'App':
                $platform_text = 'App';
                break;
        }

        return $platform_text;
    }

    public function getApplyInfoTextAttribute()
    {
        $apply_info_text = '';
        if(!empty($this->attributes['apply_info'])){
            $apply_info_arr = json_decode($this->attributes['apply_info'], true);
            $i = 0;
            if(is_array($apply_info_arr) && count($apply_info_arr)){
                foreach ($apply_info_arr as $key => $value){
                    if($i == 0){
                        $apply_info_text .= $key.':'.$value;
                    }else{
                        $apply_info_text .= PHP_EOL.$key.':'.$value;
                    }
                    $i++;
                }
            }
        }

        return nl2br($apply_info_text);
    }

    public function getApplyInfoExcelAttribute()
    {
        $apply_info_excel = '';
        if(!empty($this->attributes['apply_info'])){
            $apply_info_arr = json_decode($this->attributes['apply_info'], true);
            $i = 0;
            if(is_array($apply_info_arr) && count($apply_info_arr)){
                foreach ($apply_info_arr as $key => $value){
                    if($i == 0){
                        $apply_info_excel .= $key.':'.$value;
                    }else{
                        $apply_info_excel .= PHP_EOL.$key.':'.$value;
                    }
                    $i++;
                }
            }
        }
        return $apply_info_excel;
    }

    public function member()
    {
        return $this->hasOne(memberModel::class, 'id', 'member_id')->select(['id', 'nickname', 'realname', 'phone', 'avatar']);
    }


}